package ale.android.taxiDrivers;

import ale.android.R;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class TaxiDrivers extends ListActivity {

	private static final int ADD_ID = Menu.FIRST + 1;
	private static final int EDIT_ID = Menu.FIRST + 2;
	private static final int DELETE_ID = Menu.FIRST + 3;
	private static final int CLOSE_ID = Menu.FIRST + 4;
	private Cursor c;
	SQLiteDatabase db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initDB();
	}

	@Override
	protected void onResume() {
		super.onResume();

		c = db.query("TaxiDrivers", null, null, null, null, null,
				"ranking DESC");
		if (c.getCount() == 0) {
			Toast toast = Toast.makeText(this,
					"There are no taxi drivers stored", Toast.LENGTH_LONG);
			toast.show();
		} else {
			ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row,
					c, new String[] { "Name", "Company", "Nick", "Ranking" },
					new int[] { R.id.name, R.id.company, R.id.nick,
							R.id.ranking });

			setListAdapter(adapter);
			registerForContextMenu(getListView());

		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		db.close();
		c.close();
	}

	private void initDB() {
		TaxiDriversSQLiteHelper usdbh = new TaxiDriversSQLiteHelper(this,
				"DBTaxiDrivers", null, 1);
		db = usdbh.getWritableDatabase();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, ADD_ID, Menu.NONE, "Add").setIcon(R.drawable.add)
				.setAlphabeticShortcut('a');
		menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, "Close")
				.setIcon(R.drawable.eject).setAlphabeticShortcut('c');

		return (super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ADD_ID:
			add();
			return (true);

		case CLOSE_ID:
			finish();
			return (true);
		}

		return (super.onOptionsItemSelected(item));
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		c = (Cursor) getListAdapter().getItem(info.position);
		menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete")
				.setIcon(R.drawable.delete).setAlphabeticShortcut('d');
		menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit");

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case EDIT_ID:
			edit();
			return (true);

		case DELETE_ID:
			delete();
			return (true);

		default:

		}

		return (super.onOptionsItemSelected(item));
	}

	private void add() {
		Intent i = new Intent(this, InsertTaxiDriver.class);
		startActivity(i);
	}

	private void delete() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.delete_title)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								processDelete();
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								// ignore, just dismiss
							}
						}).show();

	}

	private void edit() {
		int ids = Integer.parseInt(c.getString(0));
		Intent i = new Intent(this, EditTaxiDriver.class);
		i.putExtra("idselected", ids);
		startActivity(i);
	}

	private void processDelete() {

		int idselected = Integer.parseInt(c.getString(0));
		db.execSQL("DELETE FROM TaxiDrivers WHERE _id = " + idselected);

		c.requery();

		if (c.getCount() == 0) {
			Toast toast = Toast.makeText(this,
					"There are no taxi drivers left", Toast.LENGTH_LONG);
			toast.show();
		}

	}
}